﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xabg.YHPLoading;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 定量装车类
    /// </summary>
    public class DLZCBLL
    {

        YHPAPI _yhpApi;
        bool IsOpen = false;
        static string _CurrentComName;
        Common.HaredWareCommonM haredWareCommonM;

        bool IsSend = false;
        static readonly object obj = new object();

        public DLZCBLL()
        {
            haredWareCommonM = IConfig.GetSetDLZC;
            OpenConnect();
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenConnect()
        {
            if (!IsOpen)
            {
                try
                {
                    _yhpApi = new YHPAPI(haredWareCommonM.PortName, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One);
                    _yhpApi.DataReceived = new Action(RefreshStatus);
                    IsOpen = _yhpApi.OpenCom();
                    if (IsOpen)
                    {
                        _yhpApi.ReadControlStatus();
                    }
                }
                catch (Exception ex)
                {
                    AutoControlTransportApiServer.AddHardWareLog(haredWareCommonM.Name, 1, ex.Message);
                    Common.Log4.LogManage.WriteErr(ex.ToString());
                    DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
                }

            }
        }

        private void RefreshStatus()
        {
        }


        /// <summary>
        /// 开启控制
        /// </summary>
        public void RunOpen()
        {
            if (IsOpen)
            {
                try
                {
                    _yhpApi?.RunOpen();
                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr(ex.ToString());
                    DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
                }
            }


        }

        /// <summary>
        /// 关闭控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RunClose()
        {
            if (IsOpen)
            {
                try
                {
                    _yhpApi?.RunClose();
                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr(ex.ToString());
                    DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
                }
            }


        }


    }
}
